import {
  Home
} from '../../models/home.js'
var home = new Home()

Page({

  data: {
    stopMore: false,
    currentPage: 1,
    bookData: [],
    bannerData: [],
    themeData: [],
    categoryData:[],
    hideMain: false
  },

  onLoad: function(options) {
    this._loadData()
  },

  showSearch(){
    this.setData({
      hideMain: true
    })
  },

  onShowHomeMain(event){
    this.setData({
      hideMain: event.detail.hide
    })
  },

  _loadData: function() {
    var that = this

    home.showLoading()
    
    home.getBannerData((res) => {
      this.setData({
        bannerData: res.data.items
      })
    })
    home.getThemeData((res) => {
      this.setData({
        themeData: res.data
      })
    })

    home.getBookData(1, (res) => {

      var books = home.compBuyPriceFromArray(res.data.data.data)
      this.setData({
        bookData: books
      })
    })

    home.getCategories((res)=>{
      this.setData({
        categoryData:res.data
      })
      wx.hideLoading()
    })
       

  },


  onReachBottom: function() {
    if (!this.data.stopMore) {
      home.showLoading()
      var page = this.data.currentPage + 1
      home.getBookData(page, (res) => {
        if (home.processRes(res.statusCode)) {
          // updataList 会判断  是否空
          if(res.data.data.data){
            res.data.data.data = home.compBuyPriceFromArray(res.data.data.data)
          }
          var result = home.updataList(res.data, this.data.bookData)
          this.setData({
            stopMore: result.stopMore,
            bookData: result.data,
            currentPage: result.currentPage
          })
        } 
        wx.hideLoading()
      })
    } else {
      home.toast("没有更多了", "none")
    }
  }
})